package com.example.user.service;

import com.example.user.bo.Person01BO;
import com.example.user.mapper.ds1.UserMapper1;
import com.example.user.mapper.ds2.UserMapper2;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserService {
    @Autowired
    //@Resource
    private UserMapper1 userMapper1;
    @Autowired
    private UserMapper2 userMapper2;

    public List<Person01BO> getList1() {
        List<Person01BO> allPerson01BO = userMapper1.getAllUser();
        return allPerson01BO;
    }

    public List<Person01BO> getList2() {
        List<Person01BO> allPerson01BO = userMapper2.getAllUser();
        return allPerson01BO;
    }

    @Transactional(propagation = Propagation.REQUIRED,value = "transactionManager1")
    public void insert(Person01BO person01BO) {
        System.out.println("testInsert");
        userMapper1.insert(person01BO);
        Person01BO bo2 = new Person01BO();
        bo2.setSapName("李四");
        bo2.setId(person01BO.getId()+"2");
        insert2(bo2);
    }
    private void insert2(Person01BO bo2){
        userMapper1.insert(bo2);
    }

    //@Cacheable(value = "Test.select", key="'testSec'+#id")
    public List<Person01BO> select(String id) {
        List<Person01BO> allPerson01BO = userMapper1.select(id);
        System.out.println("service层查询得到:" + allPerson01BO);
        return allPerson01BO;
    }

    //Cacheable(value = "rlPerson", key = "'sapId='+#sapId ")
    public List<Person01BO> check(String sapId) {
        List<Person01BO> allPerson01BO = userMapper1.check(sapId);
        return allPerson01BO;
    }

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Transactional
//@Cacheable(value = "rlPerson", key = "'sapId='+#sapId ")
    public Person01BO check1(String sapId) {
        Person01BO allPerson01BO =
                sqlSessionTemplate.selectOne("com.example.demo.mapper.userMapper1.check", sapId);
        return allPerson01BO;
    }
}
